﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassEntities;
using System.Collections;
using DevExpress.XtraScheduler;

namespace Edu_Examination
{
    public partial class UC_LichThi : UserControl
    {
        private tblSinhVien Session;
        public UC_LichThi(tblSinhVien Session)
        {
            this.Session = Session;
            InitializeComponent();
            loadData();
        }
        DataTable tblDangKyLop;
        DataTable tblToChucThi;
        DataTable tblKyThi;
        DataTable tblDeThi;


        void loadData()
        {
            Hashtable hash = new Hashtable();
            hash["idSV"] = Session.Stt;
            tblDangKyLop = Lib.Mapping.ToDataTable(Lib.doConnection.SuperFacade.gettblDangKyLop(hash));
            if (tblDangKyLop != null && tblDangKyLop.Rows.Count > 0)
            {
                string idDKLs = "";
                foreach (DataRow row in tblDangKyLop.Rows)
                {
                    idDKLs += "'" + row["IdLH"].ToString() + "',";
                }
                if (idDKLs.Length > 0)
                {
                    idDKLs = idDKLs.Substring(0, idDKLs.Length - 1);
                    hash = new Hashtable();
                    hash["IdLopHoc"] = new string[] { " in ", "(" + idDKLs + ")" };
                    tblToChucThi = Lib.Mapping.ToDataTable(Lib.doConnection.SuperFacade.gettblToChucThi(hash));
                    loadLopHoc();
                    if (tblToChucThi != null && tblToChucThi.Rows.Count > 0)
                    {
                        string idkythis = "";
                        foreach (DataRow row in tblToChucThi.Rows)
                        {
                            idkythis += "'" + row["IdKyThi"].ToString() + "',";
                        }
                        if (idkythis.Length > 0)
                        {
                            idkythis = idkythis.Substring(0, idkythis.Length - 1);
                            hash = new Hashtable();
                            hash["stt"] = new string[] { " in ", "(" + idkythis + ")" };
                            loadKyThi(hash);
                        }
                    }
                }
            }
        }
        void loadLopHoc()
        {
            lvlLopDaDK.Items.Clear();
            if (tblDangKyLop != null && tblDangKyLop.Rows.Count > 0)
            {
                foreach (DataRow row in tblDangKyLop.Rows)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = string.IsNullOrEmpty(row["Ten"].ToString()) ? "Không tên" : row["Ten"].ToString();
                    item.Tag = row["IdLH"].ToString();
                    lvlLopDaDK.Items.Add(item);
                }
            }
        }
        void loadToChucThi(string lopid)
        {
            if (tblToChucThi != null && tblToChucThi.Rows.Count > 0)
            {
                Hashtable hash;
                string idkythis = "";
                DataRow[] rows = tblToChucThi.Select("IdLopHoc = '" + lopid + "'");
                foreach (DataRow row in rows)
                {
                    idkythis += "'" + row["IdKyThi"].ToString() + "',";
                }
                if (idkythis.Length > 0)
                {
                    idkythis = idkythis.Substring(0, idkythis.Length - 1);
                    hash = new Hashtable();
                    hash["stt"] = new string[] { " in ", "(" + idkythis + ")" };
                    loadKyThi(hash);
                }
            }
        }
        public void loadKyThi(Hashtable gethash)
        {
            lvlMonThi.Items.Clear();
            schedulerStorage1.Appointments.Clear();
            tblKyThi = Lib.Mapping.ToDataTable(Lib.doConnection.SuperFacade.gettblKyThi(gethash));
            Hashtable hash;
            if (tblToChucThi != null)
            {
                if (tblToChucThi.Rows.Count > 0)
                {
                    foreach (DataRow row in tblToChucThi.Rows)
                    {
                        hash = new Hashtable();
                        hash["stt"] = new string[] { " in ", "(" + Lib.Mapping.mappingId(row["DsDeThi"].ToString()) + ")" };
                        tblDeThi = Lib.Mapping.ToDataTable(Lib.doConnection.SuperFacade.gettblDeThi(hash));
                        if (tblDeThi != null && tblDeThi.Rows.Count > 0)
                        {
                            ListViewItem item = new ListViewItem();
                            int timethi = int.Parse(tblDeThi.Rows[0]["thoigianlambai"].ToString());
                            string giothi = row["giothi"].ToString();
                            string subject = tblDeThi.Rows[0]["tenMH"].ToString();
                            item.Text = subject;
                            DateTime dateStart = DateTime.Parse(((DateTime)row["ngaythi"]).ToString("MM/dd/yyyy") + " " + giothi);
                            item.Tag = dateStart;
                            item.SubItems.Add(((DateTime)row["ngaythi"]).ToString("dd/MM/yyyy"));
                            item.SubItems.Add(giothi);
                            int gio, phut, giay;
                            gio = 0;
                            phut = timethi;
                            giay = 0;
                            if (timethi >= 60) { gio = timethi / 60; phut = timethi % 60; }
                            string timeTmp = gio + ":" + phut + ":" + giay;
                            TimeSpan tspan = new TimeSpan(gio, phut, giay);
                            DateTime timeEnd = dateStart + tspan;
                            AddSchedulerStorage(subject, dateStart, timeEnd);
                            lvlMonThi.Items.Add(item);
                        }
                    }
                }
            }
        }

        public void AddSchedulerStorage(string subject, DateTime dateStart, DateTime dateEnd)
        {
            Appointment apt = new Appointment();
            apt.Subject = subject;
            apt.Start = dateStart;
            apt.End = dateEnd;
            if (dateStart > DateTime.Now) apt.LabelId = 2;
            schedulerStorage1.Appointments.Add(apt);
        }

        private void lvlMonThi_MouseClick(object sender, MouseEventArgs e)
        {
            ListViewItem item = lvlMonThi.GetItemAt(e.X, e.Y);
            if (item != null)
            {
                item.Selected = true;
                schedulerControl1.GoToDate((DateTime)item.Tag, SchedulerViewType.Day);
            }
            //
        }

        private void lvlLopDaDK_MouseClick(object sender, MouseEventArgs e)
        {
            ListViewItem item = lvlLopDaDK.GetItemAt(e.X, e.Y);
            if (item != null)
            {
                item.Selected = true;
                loadToChucThi(item.Tag.ToString());
                schedulerControl1.ActiveViewType = SchedulerViewType.Month;
                schedulerControl1.GoToToday();
            }
        }
    }
}
